﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CallCenter.DAL;
using CallCenter.Model;

namespace CallCenter.BLL
{
    public class SeatsBLL
    {
        SeatsDAL dal = new SeatsDAL();

        #region 1.验证登录  public bool VerifyUser(string inputUserName, string inputPwd)
        /// <summary>
        /// 验证登录
        /// </summary>
        /// <param name="inputUserName"></param>
        /// <param name="inputPwd"></param>
        /// <returns></returns>
        public bool VerifyUser(string inputUserName, string inputPwd)
        {
            if (inputUserName != "" && inputPwd != "")
            {
                SeatsModel Model = dal.GetByUserName(inputUserName);
                if (Model != null && Model.Password == MD5Helper.GetMD5(inputPwd))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        } 
        #endregion

        #region 2.根据用户名获得Seat实体类的ID  public Guid GetIdByUserName(string userName)
        /// <summary>
        /// 根据用户名获得Seat实体类的ID
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public Guid GetIdByUserName(string userName)
        {
            SeatsModel model = dal.GetByUserName(userName);
            return model.Id;
        } 
        #endregion

        #region 3.根据ID,旧密码，新密码更改数据库里的Seat里的密码   public bool ChangePassword(Guid id, string oldPwd, string newPwd)
        /// <summary>
        /// 根据ID,旧密码，新密码更改数据库里的Seat里的密码
        /// </summary>
        /// <param name="id"></param>
        /// <param name="oldPwd"></param>
        /// <param name="newPwd"></param>
        /// <returns></returns>
        public bool ChangePassword(Guid id, string oldPwd, string newPwd)
        {
            SeatsModel model = dal.GetById(id);
            if (model == null)
            {
                throw new Exception("ID不存在！");
            }
            else if (model.Password != MD5Helper.GetMD5(oldPwd))
            {
                return false;
            }
            else
            {
                model.Password = MD5Helper.GetMD5(newPwd);
                dal.UpdatePassword(model);
                return true;
            }
        } 
        #endregion
    }
}
